﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Media;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using intelliPaper.Model;

namespace intelliPaper.Chart
{
    public class Charts
    {
        static public List<KeyValuePair<string, double>> GetErrorRates(List<HistoryMessage> historyMessages)
        {
            List<KeyValuePair<string, double>> keyvalues = new List<KeyValuePair<string, double>>();

            foreach (HistoryMessage historyMessage in historyMessages)
            {
                keyvalues.Add(new KeyValuePair<string, double>(historyMessage.RecordDate.ToShortDateString(), historyMessage.ErrorRate));
            }

            return keyvalues;
        }


        static public List<KeyValuePair<string, int>> GetKnowledgePointQuestions(Paper paper)
        {
            List<KeyValuePair<string, int>> keyvalues = new List<KeyValuePair<string, int>>();
            KnowledgePointComparer kpc=new KnowledgePointComparer ();

            foreach (KnowledgePoint k in paper.KnowledgePoints)
            {
                var m = from n in paper.Questions where n.KnowledgePoints.Contains(k) select n;
                keyvalues.Add (new KeyValuePair<string,int> (k.KnowledgePointBody.Substring (0,5)+"...",m==null?0:m.Count ()));
            }

            return keyvalues;
        }

        static public List<KeyValuePair<string, int>> GetKnowledgePointPoints(Paper paper,Dictionary<string ,int> points )
        {
            List<KeyValuePair<string, int>> keyvalues = new List<KeyValuePair<string, int>>();

            foreach (KnowledgePoint k in paper.KnowledgePoints)
            {
                var m = from n in paper.Questions where n.KnowledgePoints.Contains(k) select n;
                int p = 0;
                foreach (Question q in m)
                {
                    p += points[q.QuestionType];
                }
                keyvalues.Add (new KeyValuePair<string,int> (k.KnowledgePointBody.Substring (0,5)+"...",p));
            }

            return keyvalues ;
        }
    }
    class KnowledgePointComparer : IEqualityComparer<KnowledgePoint>
    {
        public bool Equals(KnowledgePoint k1, KnowledgePoint k2)
        {
            if (k1.ID == k2.ID)
                return true;
            else
                return false;
        }

        public int GetHashCode(KnowledgePoint k)
        {
            int hCode = k.ID;
            return hCode.GetHashCode();
        }
    }
}
